﻿using LxCore;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace LxCore
{
    public partial class PhysicsCenter : JsObject
    {
        private TimeDiscreteTypes _TimeDiscreteType = TimeDiscreteTypes.Steady;
        public TimeDiscreteTypes TimeDiscreteType
        {
            get => _TimeDiscreteType;
            set
            {
                _TimeDiscreteType = value;
                RaisePropertyChanged(nameof(TimeDiscreteType));
                RaisePropertyChanged(nameof(IsSteady));
                RaisePropertyChanged(nameof(IsTransient));
            }
        }
        public bool IsSteady => _TimeDiscreteType == TimeDiscreteTypes.Steady;
        public bool IsTransient => !IsSteady;
        public bool IsExplicitTrans => _TimeDiscreteType == TimeDiscreteTypes.Explicit_1st || _TimeDiscreteType == TimeDiscreteTypes.Explicit_2nd;
        public bool IsImplicitTrans => _TimeDiscreteType == TimeDiscreteTypes.Implicit_1st || _TimeDiscreteType == TimeDiscreteTypes.Implicit_2nd;
        private ParameterBase<double> _TimeInterval = new ConstField.Double(0.1);
        public ParameterBase<double> TimeInterval
        {
            get { return _TimeInterval; }
            set { _TimeInterval = value; RaisePropertyChanged(nameof(TimeInterval)); }
        }
        public MaterialLib MaterialLib { get; set; } = new MaterialLib();
        public PhysicalModelList PhysicalModels { get; set; } = new();
        public PhysicalModel CreatePhysicalModel()
        {
            PhysicalModel physical = new PhysicalModel();
            PhysicalModels.Add(physical);
            return physical;
        }

        public override void ToJsonKV(IJsonWriter writer)
        {
            base.ToJsonKV(writer);
            writer.WriteEnum(nameof(TimeDiscreteType), TimeDiscreteType);
            if (IsTransient)
            {
                writer.WriteObject(nameof(TimeInterval), TimeInterval, true);
            }
            writer.WriteList(nameof(PhysicalModels), PhysicalModels);
        }
        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(TimeDiscreteType): TimeDiscreteType = jsReader.ReadEnum<TimeDiscreteTypes>(); break;
                case nameof(TimeInterval): jsReader.ReadObject(TimeInterval, e => TimeInterval = e); break;
                case nameof(PhysicalModels): jsReader.ReadList(PhysicalModels, t => new PhysicalModel()); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public class PhysicalModelList : JsIdentifyList<PhysicalModel>
        {
            //protected override bool CanRemove(PhysicalModel item)
            //{
            //    foreach (var zs in LxSim.Sim.Conditions.ZoneSets)
            //    {
            //        if (Equals(item, zs.PhysicalModel))
            //        {
            //            LxLog.Error($"{item.Name}正在使用中，无法删除");
            //            return false;
            //        }
            //    }
            //    return true;
            //}
        }
    }
}
